package com.monkeygroup.mygoplus.common.DefinedView;

import android.app.Dialog;
import android.content.Context;
import android.media.Image;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;

/**
 * Created by Administrator on 2016/9/25.
 */
public class MyDialog extends Dialog {
    private OnNewFriendApplyListener myNewFriendListener = null;
    private OnUserDetailListener myDetailListener = null;
    private OnFriendExchangeListener myExchangeListener = null;
    private OnCommonClickListener myClickListener = null;
    private OnCommonDoubleClickListener myDoubleClickListener = null;
    private OnThreeButtonClickListener myThreeClickListener = null;

    public MyDialog(Context context) {
        super(context);
    }

    public MyDialog(Context context, int theme) {
        super(context, theme);
    }

    /**
     * 點擊好友申請，顯示同意或拒絕的dialog
     **/
    public MyDialog(Context context, int theme, final String userid, OnNewFriendApplyListener newFriendListener) {
        super(context, theme);
        this.myNewFriendListener = newFriendListener;

        init();
        getWindow().setContentView(R.layout.dialog_friend_apply_handle);
        getWindow().findViewById(R.id.new_friend_agree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myNewFriendListener != null) {
                    myNewFriendListener.doAgree(userid);
                }
            }
        });
        getWindow().findViewById(R.id.new_friend_ignore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myNewFriendListener != null) {
                    myNewFriendListener.doRefuse(userid);
                }
            }
        });

    }

    /**
     * 點擊自己的頭像，顯示資料dialog
     **/
    public MyDialog(Context context, int theme, final String userid, OnUserDetailListener userDetailListener) {
        super(context, theme);
        this.myDetailListener = userDetailListener;

        init();
        getWindow().setContentView(R.layout.dialog_personal_info);
        TextView item_name = (TextView) getWindow().findViewById(R.id.item_name);
        item_name.setText(MyGoApplication.myNickname);
        TextView item_id = (TextView) getWindow().findViewById(R.id.item_id);
        item_id.setText(MyGoApplication.myECUserid);
        getWindow().findViewById(R.id.btn_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (myDetailListener != null) {
                    myDetailListener.doUserDetail(userid);
                }
            }
        });
    }

    /**
     * 陌生人資訊，加為好友
     **/
    public MyDialog(Context context, int theme, final String userid, OnCommonClickListener commonClickListener) {
        super(context, theme);
        this.myClickListener = commonClickListener;

        init();
        getWindow().setContentView(R.layout.dialog_shared_user);
        getWindow().findViewById(R.id.btn_add_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (myClickListener != null) {
                    myClickListener.doClick(userid);
                }
            }
        });
    }

    /**
     * 點擊好友頭像，顯示可操作的項dialog
     **/
    public MyDialog(Context context, int theme, boolean isFavorite, OnFriendExchangeListener userExchangeListener) {
        super(context, theme);
        this.myExchangeListener = userExchangeListener;

        init();
        getWindow().setContentView(R.layout.dialog_friend_exchange);
        ImageView contact_favorite_state = (ImageView) getWindow().findViewById(R.id.contact_favorite_state);
        if (isFavorite) {
            contact_favorite_state.setImageResource(R.drawable.bt_chat_favorite_ok);
        } else {
            contact_favorite_state.setImageResource(R.drawable.bt_chat_favorite_invalid);
        }
        contact_favorite_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myExchangeListener != null) {
                    ImageView view = (ImageView) v;
                    myExchangeListener.doFavorite(view);
                }
            }
        });

        getWindow().findViewById(R.id.layout_start_message).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myExchangeListener != null) {
                    myExchangeListener.doMessage();
                }
            }
        });
        getWindow().findViewById(R.id.layout_start_call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myExchangeListener != null) {
                    myExchangeListener.doCall();
                }
            }
        });

        getWindow().findViewById(R.id.layout_start_video).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myExchangeListener != null) {
                    myExchangeListener.doVideo();
                }
            }
        });

    }

    /**
     * 普通的message消息，左右雙button
     **/
    public MyDialog(Context context, int theme, String message, String left_text, String right_text, OnCommonDoubleClickListener commonDoubleClickListener) {
        super(context, theme);
        this.myDoubleClickListener = commonDoubleClickListener;

        init();
        getWindow().setContentView(R.layout.dialog_common_double_click);
        TextView contentView = (TextView) getWindow().findViewById(R.id.dialog_message);
        contentView.setText(message);
        TextView btn_left = (TextView) getWindow().findViewById(R.id.btn_left);
        btn_left.setText(left_text);
        TextView btn_right = (TextView) getWindow().findViewById(R.id.btn_right);
        btn_right.setText(right_text);
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myDoubleClickListener != null) {
                    myDoubleClickListener.doLeftClick();
                }
            }
        });

        btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myDoubleClickListener != null) {
                    myDoubleClickListener.doRightClick();
                }
            }
        });
    }

    /**
     * 普通的message消息，單button
     **/
    public MyDialog(Context context, int theme, String message, String button_text, OnCommonClickListener commonClickListener) {
        super(context, theme);
        this.myClickListener = commonClickListener;

        init();
        getWindow().setContentView(R.layout.dialog_common_single_click);
        TextView contentView = (TextView) getWindow().findViewById(R.id.dialog_message);
        contentView.setText(message);
        TextView buttonView = (TextView) getWindow().findViewById(R.id.button_confirm);
        buttonView.setText(button_text);
        buttonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myClickListener != null) {
                    myClickListener.doClick(null);
                }
            }
        });
    }

    /**
     * 三个button的dialog
     **/
    public MyDialog(Context context, int theme, String text1, String text2, String text3, OnThreeButtonClickListener threeButtonClickListener) {
        super(context, theme);
        this.myThreeClickListener = threeButtonClickListener;

        show();
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        lp.width = MyGoApplication.fullScreenWidth - 80;
        dialogWindow.setAttributes(lp);

        getWindow().setContentView(R.layout.popup_layout_choose);
        TextView dialog_btn_one = (TextView) getWindow().findViewById(R.id.dialog_btn_one);
        dialog_btn_one.setText(text1);
        TextView dialog_btn_two = (TextView) getWindow().findViewById(R.id.dialog_btn_two);
        dialog_btn_two.setText(text2);
        TextView dialog_btn_three = (TextView) getWindow().findViewById(R.id.dialog_btn_three);
        dialog_btn_three.setText(text3);
        dialog_btn_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myThreeClickListener != null) {
                    myThreeClickListener.firstClick();
                }
            }
        });

        dialog_btn_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myThreeClickListener != null) {
                    myThreeClickListener.secondClick();
                }
            }
        });

        dialog_btn_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myThreeClickListener != null) {
                    myThreeClickListener.thirdClick();
                }
            }
        });

        getWindow().findViewById(R.id.dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 初始化dialog，設置位置和寬度
     **/
    private void init() {
        show();

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        lp.width = MyGoApplication.fullScreenWidth - 120;
        dialogWindow.setAttributes(lp);
    }

    /**
     * 新朋友申請監聽
     **/
    public interface OnNewFriendApplyListener {
        void doAgree(String userid);

        void doRefuse(String userid);
    }

    /**
     * 用戶詳情監聽
     **/
    public interface OnUserDetailListener {
        void doUserDetail(String userid);
    }

    /**
     * 與朋友互動的監聽
     **/
    public interface OnFriendExchangeListener {
        void doMessage();

        void doCall();

        void doVideo();

        void doFavorite(ImageView view);
    }

    /**
     * 通用雙button message點擊監聽
     **/
    public interface OnCommonDoubleClickListener {
        void doLeftClick();

        void doRightClick();
    }

    /**
     * 通用單button message點擊監聽
     **/
    public interface OnCommonClickListener {
        void doClick(String var1);
    }

    /**
     * 通用三个button點擊監聽
     **/
    public interface OnThreeButtonClickListener {
        void firstClick();

        void secondClick();

        void thirdClick();
    }

}